package com.sunlg.smarthome.ui.plant;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.sunlg.smarthome.R;
import com.sunlg.smarthome.base.BaseActivity;
import com.sunlg.smarthome.contract.AddPumpContract;
import com.sunlg.smarthome.contract.AddRainObserverContract;
import com.sunlg.smarthome.contract.presenter.AddPumpPresenter;
import com.sunlg.smarthome.contract.presenter.AddRainObserverPresenter;
import com.sunlg.smarthome.data.entity.Pump;
import com.sunlg.smarthome.data.entity.RainObserver;

import java.util.ArrayList;
import java.util.List;

public class PlantManageActivity extends BaseActivity
        implements AddPumpContract.View, AddRainObserverContract.View{

    private ViewPager mViewPager;
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mFragments;

    private AddPumpContract.Presenter addPumpPresenter;
    private AddRainObserverContract.Presenter addRainObserverPresenter;

    private PumpListFragment pumpListFragment;
    private RainObserverFragment rainObserverFragment;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_plant_manage;
    }

    @Override
    protected void setupToolBar(Toolbar toolbar) {
        super.setupToolBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("植物管理");
        actionBar.setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doFinish();
            }
        });
    }

    @Override
    protected void init() {
        addPumpPresenter = new AddPumpPresenter();
        addRainObserverPresenter = new AddRainObserverPresenter();
        addPumpPresenter.attachView(this);
        addRainObserverPresenter.attachView(this);

        pumpListFragment = new PumpListFragment();
        rainObserverFragment = new RainObserverFragment();
    }

    @Override
    protected void initView() {
        mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
        mFragments = new ArrayList<>();
        mFragments.add(pumpListFragment);
        mFragments.add(rainObserverFragment);

        List<String> tiles=new ArrayList<>();
        tiles.add("水泵");
        tiles.add("降雨");

        mAdapter=new PlantViewPagerAdapter(getSupportFragmentManager(),mFragments,tiles);
        mViewPager.setAdapter(mAdapter);
        TabLayout mTabLayout = (TabLayout) findViewById(R.id.tabLayout);
        mTabLayout.addTab(mTabLayout.newTab().setText("水泵"));//给TabLayout添加Tab
        mTabLayout.addTab(mTabLayout.newTab().setText("降雨"));

        //给TabLayout设置关联ViewPager，如果设置了ViewPager，那么ViewPagerAdapter中的getPageTitle()方法返回的就是Tab上的标题

        mTabLayout.setupWithViewPager(mViewPager);//给TabLayout设置关联ViewPager，如果设置了ViewPager，那么
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_plant_manage, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == R.id.action_add_pump){
            addPump();
        }else if(item.getItemId() == R.id.action_add_rain_observer){
            addRainObserver();
        }
        return super.onOptionsItemSelected(item);
    }

    private void addPump(){
        AddPumpDialog dialog = new AddPumpDialog(this);
        dialog.setOnEditTextInputListener(new AddPumpDialog.OnEditTextInputListener() {
            @Override
            public void onEditTextInput(Integer no, String inputText) {
                Pump pump = new Pump();
                pump.setDevice(no);
                pump.setDescription(inputText);
                pump.setStatus(0);
                addPumpPresenter.addPump(pump);
            }
        });
        dialog.show();
    }

    private void addRainObserver(){
        AddRainObserverDialog dialog = new AddRainObserverDialog(this);
        dialog.setOnEditTextInputListener(new AddRainObserverDialog.OnEditTextInputListener() {
            @Override
            public void onEditTextInput(Integer no, String inputText) {
                RainObserver rainObserver = new RainObserver();
                rainObserver.setDevice(no);
                rainObserver.setDescription(inputText);
                rainObserver.setStatus(0);
                addRainObserverPresenter.addRainObserver(rainObserver);
            }
        });
        dialog.show();
    }

    @Override
    public void addRainObserverSuccess() {
        mViewPager.setCurrentItem(1);
    }

    @Override
    public void addPumpSuccess() {
        pumpListFragment.presenter.loadAllPump();
    }

    @Override
    public void showError(String msg) {
        toast(msg);
    }
}
